package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.shopping.IProductService;
import com.mall.shopping.constant.GlobalConstants;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service(interfaceClass = IProductService.class)
@Component
@Slf4j
public class ProductServiceImpl implements IProductService {
    @Autowired
    ItemMapper itemMapper;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    ContentConverter contentConverter;

    @Autowired
    ItemDescMapper itemDescMapper;





    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {

        ProductDetailResponse response = new ProductDetailResponse();

        try {
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            ProductDetailDto productDetailDto = productConverter.productDto2detail(item);

            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());
            productDetailDto.setDetail(itemDesc.getItemDesc());
            productDetailDto.setProductImageBig(item.getImageBig());


            productDetailDto.setProductImageSmall(Arrays.asList(item.getImages()));
            response.setProductDetailDto(productDetailDto);

        } catch (Exception e) {
            response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
            log.info("IProductServiceImpl.getProductDetail occur Exception:{}", e);
        }
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return response;
    }

    // 分页查询商品列表

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        // 分页查询条件
        Integer page = request.getPage();
        Integer size = request.getSize();

        PageHelper.startPage(page, size);

        //分类ID
        Long cid = request.getCid();

        //最低价格,查询条件
        Integer priceGt = request.getPriceGt();
        //最高价格
        Integer priceLte = request.getPriceLte();

        // 只有通过价格排序一种方式,从低到高为1,从高到低为-1
        String sort = request.getSort();

        Example example = new Example(Item.class);

        // 排序
        if (sort != null && !"".equals(sort)) {
            if ("1".equals(sort)) {
                sort = "asc";
            }
            if ("-1".equals(sort)) {
                sort = "desc";
            }
        }
        example.setOrderByClause(" price " + sort);

        Example.Criteria criteria = example.createCriteria();

        if (cid != null && !"".equals(cid)) {
            criteria.andEqualTo("cid", cid);
        }

        if (priceGt != null && !"".equals(priceGt)) {
            criteria.andGreaterThanOrEqualTo("price", priceGt);
        }

        if (priceLte != null && !"".equals(priceLte)) {
            criteria.andLessThanOrEqualTo("price", priceLte);
        }
        // 过滤下架商品 1 代表商品
        criteria.andEqualTo("status", 1);
        List<Item> items = itemMapper.selectByExample(example);

        List<ProductDto> productDtos = productConverter.items2Dto(items);

        PageInfo<Item> itemPageInfo = new PageInfo<>(items);
        long total = itemPageInfo.getTotal();

        AllProductResponse resp = new AllProductResponse();
        resp.setProductDtoList(productDtos);
        resp.setTotal(total);

        return resp;
    }

    @Override
    public RecommendResponse getRecommendGoods() {

        Panel panel = panelMapper.selectByPrimaryKey(GlobalConstants.RECOMMEND_PANEL_ID);


        List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
        panel.setPanelContentItems(panelContentItems);

        PanelDto panelDto = contentConverter.panen2Dto(panel);

        RecommendResponse resp = new RecommendResponse();

        Set<PanelDto> panelDtos = new HashSet<>();
        panelDtos.add(panelDto);
        resp.setPanelContentItemDtos(panelDtos);
        return resp;
    }
}
